package ui;
import ht_birds.hyper_birdsCountry;
import ht_birds.hyper_birdsName;
import ht_birds.hyper_birdsROE;

import java.awt.event.ActionEvent;
import java.sql.SQLException;

import javax.swing.AbstractAction;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.LayoutStyle;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class hyperbolicTreeWindow extends javax.swing.JFrame {
	private JLabel labellabel;
	private JButton generateHyperTree;
	private AbstractAction generateTree;
	private JComboBox leComboBox;
	private static String[] goku = new String[] { "Country" , "Scientific Name" , "Risk of Extinction" };

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				hyperbolicTreeWindow inst = new hyperbolicTreeWindow();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public hyperbolicTreeWindow() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				labellabel = new JLabel();
				labellabel.setText("Select the desired attribute to generate an Hyperbolic Tree:");
			}
			{
				ComboBoxModel leComboBoxModel = 
					new DefaultComboBoxModel(
							goku);
				leComboBox = new JComboBox();
				leComboBox.setModel(leComboBoxModel);
			}
			{
				generateHyperTree = new JButton();
				generateHyperTree.setText("Generate Hyperbolic Tree");
				generateHyperTree.setAction(getGenerateTree());
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(labellabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(leComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(generateHyperTree, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(24, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(labellabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 352, GroupLayout.PREFERRED_SIZE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(leComboBox, GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 132, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(generateHyperTree, GroupLayout.PREFERRED_SIZE, 220, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 132, Short.MAX_VALUE))));
			pack();
			this.setSize(380, 154);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private AbstractAction getGenerateTree() {
		if(generateTree == null) {
			generateTree = new AbstractAction("Generate Hyperbolic Tree", null) {
				public void actionPerformed(ActionEvent evt) {
					successDialog sD = new successDialog(hyperbolicTreeWindow.this);
					boolean error = false;
					String[] s = new String[] {  };
					
					if (leComboBox.getSelectedItem().toString().equals(goku[0])) {
						try {
							hyper_birdsCountry.main(s);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							error = true;
							e.printStackTrace();
						}
					} else {
						if (leComboBox.getSelectedItem().toString().equals(goku[1])) {
							try {
								hyper_birdsName.main(s);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								error = true;
								e.printStackTrace();
							}
						} else {
							try {
								hyper_birdsROE.main(s);
							} catch (SQLException e) {
								error = true;
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
					if ( error ) {
						sD.setLabelText("An error ocurred while generating the hyper tree.");
						sD.pack();
						sD.setLocationRelativeTo(null);
						sD.setVisible(true);
					}
						
				}
			};
		}
		return generateTree;
	}

}
